package com.yeren.service;

import net.sf.json.JSONObject;

import com.yeren.dto.TokenDTO;
import com.yeren.dto.WeixinOauth2TokenDTO;
import com.yeren.utils.CommonUtil;

/**
 * liubiao
 */
public class GetTokenService {

	// private static Logger log = LoggerFactory.getLogger(AdvancedUtil.class);

	/**
	 * 获取接口访问凭证
	 * 
	 * @param appid
	 * @param appsecret
	 * @return Token
	 */
	public static TokenDTO getToken(String appid, String appsecret) {
		TokenDTO token = null;
		String token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
		String requestUrl = token_url.replace("APPID", appid).replace(
				"APPSECRET", appsecret);
		// 发起GET请求获取凭据
		JSONObject jsonObject = CommonUtil
				.httpsRequest(requestUrl, "GET", null);

		if (null != jsonObject) {
			try {
				token = new TokenDTO();
				token.setAccessToken(jsonObject.getString("access_token"));
				token.setExpiresIn(jsonObject.getInt("expires_in"));
			} catch (net.sf.json.JSONException e) {
				token = null;
				// 获取token失败
				// log.error("获取token失败errcode:{ } errmsg:{ }",jsonObject.getInt("errcode"),jsonObject.getString("errmsg"));
			}
		}
		return token;

	}

	/**
	 * 获取网页授权凭证
	 * 
	 * @param appId
	 *            公众号的唯一标识
	 * @param appSecret
	 *            公众号的秘钥
	 * @param code
	 * @return WeixinOauth2Token
	 */
	public static WeixinOauth2TokenDTO getOauth2AccessToken(String appId,
			String appSecret, String code) {
		WeixinOauth2TokenDTO wat = null;
		// 拼接请求地址
		String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
		requestUrl = requestUrl.replace("APPID", appId);
		requestUrl = requestUrl.replace("SECRET", appSecret);
		requestUrl = requestUrl.replace("CODE", code);
		// 获取网页授权凭证
		JSONObject jsonObject = CommonUtil
				.httpsRequest(requestUrl, "GET", null);
		if (null != jsonObject) {
			try {
				wat = new WeixinOauth2TokenDTO();
				wat.setAccessToken(jsonObject.getString("access_token"));
				wat.setExpiresIn(jsonObject.getInt("expires_in"));
				wat.setRefreshToken(jsonObject.getString("refresh_token"));
				wat.setOpenId(jsonObject.getString("openid"));
				wat.setScope(jsonObject.getString("scope"));

			} catch (Exception e) {
				wat = null;
				int errorCode = jsonObject.getInt("errcode");
				String errorMsg = jsonObject.getString("errmsg");
				// log.error("获取网页授权凭证失败errcode:{ } errmsg:{ }",
				// errorCode,errorMsg);
			}
		}
		return wat;
	}

	/**
	 * 刷新网页授权凭据
	 * 
	 * @param appId
	 *            公众号的唯一标识
	 * @param refreshToken
	 * @return WeixinOauth2Token
	 */
	public static WeixinOauth2TokenDTO refreshOauth2AccessToken(String appId,
			String refreshToken) {
		WeixinOauth2TokenDTO wat = null;
		// 拼接请求地址
		String requestUrl = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN";
		requestUrl = requestUrl.replace("APPID", appId);
		requestUrl = requestUrl.replace("REFRESH_TOKEN", refreshToken);
		// 刷新网页授权凭据
		JSONObject jsonObject = CommonUtil
				.httpsRequest(requestUrl, "GET", null);
		if (null != jsonObject) {
			try {
				wat = new WeixinOauth2TokenDTO();
				wat.setAccessToken(jsonObject.getString("access_token"));
				wat.setExpiresIn(jsonObject.getInt("expires_in"));
				wat.setRefreshToken(jsonObject.getString("refresh_token"));
				wat.setOpenId(jsonObject.getString("openid"));
				wat.setScope(jsonObject.getString("scope"));
			} catch (Exception e) {
				wat = null;
				int errorCode = jsonObject.getInt("errcode");
				String errorMsg = jsonObject.getString("errmsg");
				// log.error("刷新网页授权凭证失败errcode:{ } errmsg:{ }",
				// errorCode,errorMsg);
			}
		}
		return wat;
	}

}
